package com.ysactive.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *
 * @Description: TODO
 * </p>
 * @ClassName BusinessExceptionHandler
 * @Author pl
 * @Date 2020/9/20
 * @Version V1.0.0
 */
@Slf4j
@ControllerAdvice
public class BusinessExceptionHandler {
    @ExceptionHandler({BusinessException.class})
    @ResponseBody//返回json格式数据
    public BaseResponse handlerException(BusinessException ex) {
        ErrorCodeEnume errorCodeEnume = ex.getErrorCodeEnume();
        return new BaseResponse(errorCodeEnume.getCode(), errorCodeEnume.getMsg(), null);
    }


    @ExceptionHandler({Exception.class})
    @ResponseBody
    public BaseResponse<?> handlerException(Exception ex) {
        log.info("[全局业务异常]\r\n业务编码：{}\r\n异常记录：{} \r\n 错误详情", ErrorCodeEnume.UNKNOW_EXCEPTION.getCode(), ErrorCodeEnume.UNKNOW_EXCEPTION.getMsg(), ex.getMessage());
        return new BaseResponse<>(ErrorCodeEnume.UNKNOW_EXCEPTION.getCode(), ErrorCodeEnume.UNKNOW_EXCEPTION.getMsg(), ex.getMessage());
    }
}